package com.hs.mobile.dao.service;

import java.util.List;

import com.hs.mobile.model.Device;
import com.hs.mobile.model.ServiceMB;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class ServiceDAOImpl implements ServiceDAO {

	SessionFactory sessionFactory;
	
	//setter sessionFactory
	/**
	 * 
	 * @param sessionFactory
	 */
	public void setSessionFactory( SessionFactory sessionFactory )
	{
		this.sessionFactory = sessionFactory;
	}
	
	//getter sessionFactory
	/**
	 * 
	 * @return sessionFactory
	 */
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;		
	}
	
	@Override
	public List getListAll() {
		// TODO Auto-generated method stub
		System.out.println("__ Get list all services __");
		Session session = getSessionFactory().openSession();
		List<ServiceMB> listServices = null;
		try {
			Criteria cr = session.createCriteria(ServiceMB.class);
			listServices = cr.list();
			System.out.println("__ get listServices successfully __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listServices;
	}

	@Override
	public Object getById(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get Service __");
		Session session = getSessionFactory().openSession();
		ServiceMB service = null;
		try {
			service = (ServiceMB) session.get(ServiceMB.class, id);
			System.out.println("__ Get Service successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return service;
	}

	@Override
	public void add(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Register Service __ ");
		ServiceMB service = ( ServiceMB ) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.save(service);
			session.getTransaction().commit();
			System.out.println("__ register successfully __");
			System.out.println("__ Your mobile can use this service from now __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Erro __");
		} finally {
			session.close();
		}		
	}

	@Override
	public void delete(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Your mobile does not continue using this service ___");
		ServiceMB service = (ServiceMB) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(service);
			session.getTransaction().commit();
			System.out.println("__ Delete service successful __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public void update(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Update __ ");
		ServiceMB service = (ServiceMB) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.update(service);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public ServiceMB findServiceById(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get Service __");
		Session session = getSessionFactory().openSession();
		ServiceMB service = null;
		try {
			service = (ServiceMB) session.get(ServiceMB.class, id);
			System.out.println("__ Get Service successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return service;
	}

	@Override
	public List<Device> getListDevices(long categoryId) {
		// TODO Auto-generated method stub
		System.out.println("__ Get List Device __");
		Session session = getSessionFactory().openSession();
		List<Device> listDevices = null;
		try {
			String sql = "SELECT * FROM DEVICE INNER JOIN REGISTER ON DEVICE.ID = REGISTER.ID_DEVICE INNER JOIN SERVICE ON SERVICE.ID_CATEGORY = REGISTER.ID_CATEGORY WHERE SERVICE.ID_CATEGORY =  " + categoryId;
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(Device.class);
	        listDevices = query.list();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listDevices;
	}
	
}
